//
//  RBBubbleVC.m
//  WaitBuyer
//
//  Created by Lan on 2018/11/19.
//  Copyright © 2018年 SummerTea. All rights reserved.
//

#import "RBBubbleVC.h"
#import <Masonry.h>
#import "RBThemeConfig.h"
#import "UIView+RBAdd.h"

@interface RBBubbleItemCell:UITableViewCell

@property(nonatomic, strong) UILabel *textL;

@end

@implementation RBBubbleItemCell

- (UILabel *)textL
{
    if (!_textL)
    {
        self.textL = ({
            UILabel *view = [UILabel new];
            view.textColor = [RBThemeConfig textBlackColor];
            view.font = [UIFont systemFontOfSize:12];
            view.numberOfLines = 1;
            view.textAlignment = NSTextAlignmentCenter;
            [self.contentView addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                [view mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.edges.mas_equalTo(0);
                }];
            }];
            view;
        });
    }
    return _textL;
}

@end

@interface RBBubbleVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)IBOutlet UIView *containerView;
@property(nonatomic, strong)IBOutlet UIImageView *triImgView;
@property(nonatomic, strong)IBOutlet UITableView *tableView;
@property(nonatomic, strong) NSArray *dataSource;

@property(nonatomic, copy) void(^block)(NSString *text, NSInteger index);

@end

@implementation RBBubbleVC

#pragma mark - Public

- (void)showFromVc: (UIViewController *)fromVc
              data: (NSArray *)data
               top: (CGFloat)top
             right: (CGFloat)right
        arrowRight: (CGFloat)arrowRight
             width: (CGFloat)width
            height: (CGFloat)height
        itemHeight: (CGFloat)itemHeight
        completion: (void(^)(NSString *text, NSInteger index))completion
{
    self.providesPresentationContextTransitionStyle = YES;
    self.definesPresentationContext = YES;
    self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [fromVc presentViewController:self animated:NO completion:nil];
    
    CGFloat triW = 11;
    CGFloat triH = 7;
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(height + triH);
        make.top.mas_equalTo(top);
        make.right.mas_equalTo(-right);
    }];
    [self.triImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(triW);
        make.height.mas_equalTo(triH);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(-(arrowRight - triW/2));
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.mas_equalTo(0);
        make.top.mas_equalTo(self.triImgView.mas_bottom);
    }];
    
    self.tableView.rowHeight = itemHeight;
    
    self.block = completion;
    self.dataSource = data;
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RBBubbleItemCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell)
    {
        cell = [[RBBubbleItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.textL.text = self.dataSource[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.block)
    {
        self.block(self.dataSource[indexPath.row], indexPath.row);
    }
    [self dismissViewControllerAnimated:NO completion:nil];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    if (!CGRectContainsPoint(self.containerView.frame, point))
    {
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}

@end
